﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Controllers;
using DVSProject.Models;
using DVSProject.Lib;
namespace DVSProject.Controllers
{
    public class ModelController : Controller
    {
        //
        // GET: /Model/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ModelRepo modelRepo = new ModelRepo();
            IEnumerable<ModelModels> models = modelRepo.GetModel();
            return View(models);
        }

        //
        // GET: /Model/Details/5

        public ActionResult Details(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ModelRepo modelRepo = new ModelRepo();
            ModelModels model = new ModelModels();
            model = modelRepo.GetModelById(id);

            return View(model);
        }

        //
        // GET: /Model/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            return View();
        }

        //
        // POST: /Model/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {

            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add insert logic here


                ModelRepo modelRepo = new ModelRepo();
                ModelModels model = new ModelModels();
                model.NameTh = collection["NameTh"];
                model.NameEng = collection["NameEng"];
                model.RentBasic = (!String.IsNullOrEmpty(collection["RentBasic"])) ? Convert.ToDecimal(collection["RentBasic"]) : 0;
                model.RentForUse = (!String.IsNullOrEmpty(collection["RentForUse"])) ? Convert.ToDecimal(collection["RentForUse"]) : 0;
                model.ConversionRate = (!String.IsNullOrEmpty(collection["ConversionRate"])) ? Convert.ToInt32(collection["ConversionRate"]) : 1;
                model.CreateBy = (string)Session["User"];

                if (model.NameTh == "")
                {
                    TempData["errornameth"] = "ยังไม่ป้อนข้อมูโมเดลภาษาไทย";
                    return RedirectToAction("Create");
                }
                else if (model.NameEng == "")
                {
                    TempData["errornameeng"] = "ยังไม่ป้อนข้อมูโมเดลภาษาอังกฤษ";
                    return RedirectToAction("Create");
                }

                else
                {
                    bool result = modelRepo.Create(model);
                    return RedirectToAction("Index");
                }


            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Model/Edit/5

        public ActionResult Edit(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ModelRepo modelRepo = new ModelRepo();
            ModelModels model = new ModelModels();
            model = modelRepo.GetModelById(id);

            return View(model);
        }

        //
        // POST: /Model/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here

                ModelRepo modelRepo = new ModelRepo();
                ModelModels model = new ModelModels();
                model.ModelId = id;
                model.NameTh = collection["NameTh"];
                model.NameEng = collection["NameEng"];
                model.RentBasic = (!String.IsNullOrEmpty(collection["RentBasic"])) ? Convert.ToDecimal(collection["RentBasic"]) : 0;
                model.RentForUse = (!String.IsNullOrEmpty(collection["RentForUse"])) ? Convert.ToDecimal(collection["RentForUse"]) : 0;
                model.ConversionRate = (!String.IsNullOrEmpty(collection["ConversionRate"])) ? Convert.ToInt32(collection["ConversionRate"]) : 1;
                model.UpdateBy = (string)Session["User"];
                 if (model.NameTh == "")
                {
                    TempData["errornameth"] = "ยังไม่ป้อนข้อมูลโมเดลภาษาไทย";
                    return RedirectToAction("Edit");
                }
                 else if (model.NameEng == "")
                 {
                     TempData["errornameeng"] = "ยังไม่ป้อนข้อมูลโมเดลภาษาอังกฤษ";
                     return RedirectToAction("Edit");
                 }

                 else
                 {
                     bool result = modelRepo.Edit(model);

                     return RedirectToAction("Index");
                 }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Model/Delete/5

        public ActionResult Delete(int id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            ModelRepo modelRepo = new ModelRepo();
            ModelModels model = new ModelModels();
            model = modelRepo.GetModelById(id);

            return View(model);
        }

        //
        // POST: /Model/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                ModelRepo modelRepo = new ModelRepo();
                ModelModels model = new ModelModels();
                model.ModelId = id;
                model.UpdateBy = (string)Session["User"];
                bool result = modelRepo.Delete(model);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
